﻿namespace Xioa_UI.Domain;

public sealed class Map<T, TD> : Dictionary<T, TD> where T : IComparable
{
    /// <summary>
    /// Get Index Value
    /// </summary>
    /// <returns></returns>
    public KeyValuePair<T, TD> IndexValue()
    {
        return this.FirstOrDefault();
    }

    /// <summary>
    /// Get Index Value (Func Value)
    /// </summary>
    /// <param name="func"></param>
    /// <returns></returns>
    public KeyValuePair<T, TD> IndexValue(Func<KeyValuePair<T, TD>, bool> func)
    {
        return this.FirstOrDefault(func);
    }

    /// <summary>
    /// Get Index Value And DeleteValue
    /// </summary>
    /// <returns></returns>
    public KeyValuePair<T, TD> IndexDequeue()
    {
        var result = this.IndexValue();
        this.Delete(result.Key);
        return result;
    }

    /// <summary>
    /// Get Index Value And Delete Value (Func Value)
    /// </summary>
    /// <param name="func"></param>
    /// <returns></returns>
    public KeyValuePair<T, TD> IndexDequeue(Func<KeyValuePair<T, TD>, bool> func)
    {
        var value = this.FirstOrDefault(func);
        this.Delete(value.Key);
        return value;
    }

    /// <summary>
    /// Get Last Value
    /// </summary>
    /// <returns></returns>
    public KeyValuePair<T, TD> LastValue()
    {
        return this.LastOrDefault();
    }

    /// <summary>
    /// Get Last Value And DeleteValue
    /// </summary>
    /// <returns></returns>
    public KeyValuePair<T, TD> LastDequeue()
    {
        var result = this.LastValue();
        this.Delete(result.Key);
        return result;
    }

    /// <summary>
    /// Get Last Value (Func Value)
    /// </summary>
    /// <param name="func"></param>
    /// <returns></returns>
    public KeyValuePair<T, TD> LastValue(System.Func<KeyValuePair<T, TD>, bool> func)
    {
        return this.LastOrDefault(func);
    }

    /// <summary>
    /// Get Last Value And DeleteValue (Func Value)
    /// </summary>
    /// <param name="func"></param>
    /// <returns></returns>
    public KeyValuePair<T, TD> LastDequeue(System.Func<KeyValuePair<T, TD>, bool> func)
    {
        var value = this.LastOrDefault(func);
        this.Delete(value.Key);
        return value;
    }

    /// <summary>
    /// Get Key Value
    /// </summary>
    /// <param name="key">key</param>
    /// <returns>value</returns>
    public TD? Get(T key)
    {
        var result = this.TryGetValue(key, out var resultData);
        return resultData;
    }

    /// <summary>
    /// Add Value 
    /// </summary>
    /// <param name="key">key</param>
    /// <param name="value">value</param>
    /// <returns></returns>
    public bool Put(T? key, TD value)
    {
        return key is not null && this.TryAdd(key, value);
    }

    /// <summary>
    /// Delete
    /// </summary>
    /// <param name="key">key</param>
    /// <returns>delete result</returns>
    public bool Delete(T? key)
    {
        return key is not null && this.Remove(key);
    }

    /// <summary>
    /// Change Key's Value
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public bool ChangeValue(T? key, TD value)
    {
        var result = IndexDequeue(e => e.Key.CompareTo(key) == 0);
        return this.Put(key, value);
    }
}